import { Pipe, PipeTransform } from '@angular/core';
import { AppGlobal } from '../providers/appGlobal';

/**
 * Generated class for the DevPipe pipe.
 *
 * See https://angular.io/api/core/Pipe for more info on Angular Pipes.
 */
@Pipe({
    name: 'devPipe',
})
export class DevPipe implements PipeTransform {
    /**
     * Takes a value and makes it lowercase.
     */

    transform(value: any, ...args) {

        //设备模式
        if (args[0] == 'status') {
            let flagStr = '';
            value = '' +value;
            switch (value) {
                case '0':
                    flagStr = "停止运行"; break;
                case '1':
                    flagStr = "手动模式"; break;
                case '2':
                    flagStr = "时控模式"; break;
                case '3':
                    flagStr = "光控模式"; break;
                case '99':
                    flagStr = "设备故障"; break;
                default:
                    flagStr = value; break;
            }
            return flagStr;
        }

        //根据physicalCode 过滤数据 重新返回新的数据
        if (args[0] == 'pyFilter') {
            var arg1 = args[1];

            let isBigEnough = (element, index, array) => {
                if (!element) {
                    return false;
                }
                if ('physicalCode' in element) {
                    return (element.physicalCode.indexOf(arg1) > -1);
                }
                return false;
            }
            var newArr = value.filter(isBigEnough);
            return newArr;
            //return value;
        }


        //故障类型
        if (args[0] == 'badType') {
           
            if(!value){
                return value;
            }else{

            }
            // console.log(AppGlobal);
            let obj = JSON.parse(value);
            var list = [];
            var all =  AppGlobal.getInstance().getBadTypes();
            var str = '';
            for(let key in obj){
            //   console.log(all);
               for (let i=0;i<all.length;i++){
                 var value = all[i];
                 if (value.name == key){
                   list.push(all[i].value);
                   str = str + all[i].value + ' ';
                 }
               }
            }
            return str;
        }

        //故障状态
        if (args[0] == 'badStatus') {
            let flagStr = '';
            value = '' +value;
            switch (value) {
                case '0':
                    flagStr = "未处理"; break;
                case '1':
                    flagStr = "处理中"; break;
                case '2':
                    flagStr = "已处理"; break;
                default:
                    flagStr = value; break;
            }
            return flagStr;
        }

        //运行时间段
        if (args[0] == 'runTimeToStr') {
            let flagStr = '';
            // console.log(value);
            if(value){
                //如果是字符串，转为数组
                if('string' == typeof value) {
                    value = eval("("+value+")")
                }
                for(var i=0; i<value.length; i++){
                    flagStr +=value[i].start + "~" + value[i].end + "，"
                }
                flagStr = flagStr.substring(0, flagStr.length-1)
                return flagStr;
            }
            return value;
        }

        //天线标识
        if (args[0] == 'aerialFlag') {
            let flagStr = '';
            value = '' +value;
            switch (value) {
                case '0':
                    flagStr = "关闭"; break;
                case '1':
                    flagStr = "运行"; break;
                case '99':
                    flagStr = "故障"; break;
                default:
                    flagStr = "--";break;
            }
            return flagStr;
        }

        //太阳能状态
        if (args[0] == 'solarStatus') {
            let flagStr = '';
            value = '' +value;
            switch (value) {
                case '1':
                    flagStr = "充电中"; break;
                default:
                    flagStr = "工作中"; break;
            }
            return flagStr;
        }
        //风向
        if (args[0] == 'windDirection') {
            let flagStr = '';
            value = '' +value;
            switch (value){
                case '0': flagStr="东风";break;
                case '1': flagStr="东南风";break;
                case '2': flagStr="南风";break;
                case '3': flagStr="西南风";break;
                case '4': flagStr="西风";break;
                case '5': flagStr="西北风";break;
                case '6': flagStr="北风";break;
                case '7': flagStr="东北风";break;
            }
            return flagStr;
        }
        //灯运行状态
        if (args[0] == 'lampFlag') {
            let flagStr = '';
            value = '' +value;
            switch (value){
                case '0': flagStr="关闭";break;
                case '1': flagStr="开启";break;
                case '99': flagStr="故障";break;
            }
            return flagStr;
        }
        //风机运行状态
        if (args[0] == 'fanFlag') {
            let flagStr = '';
            value = '' +value;
            switch (value){
                case '0': flagStr="关闭";break;
                case '1': flagStr="开启";break;
                case '99': flagStr="故障";break;
            }
            return flagStr;
        }
       
        //设备运行状态
        if (args[0] == 'runFlag') {
            let flagStr = '';
            value = '' +value;
            switch (value){
                case '0': flagStr="关闭";break;
                case '1': flagStr="开启";break;
                case '99': flagStr="故障";break;
            }
            return flagStr;
        }
        if (args[0] == 'leftFlow') {
            let flagStr = '';
            value = '' +value;
            switch (value){
                case '0': flagStr="关闭";break;
                case '1': flagStr="开启";break;
                case '99': flagStr="故障";break;
            }
            return flagStr;
        }

        // console.log(args);
        return "未知";
        //return value.toLowerCase();
    }
}
